#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TraineeScoreProvider : CY.GFive.Core.Providers.DALProvider.ITraineeScoreProvider
	{
		#region Sql
		private static readonly string SqlSelectTraineeScore = "SELECT [Id], [StdCode],[ProjectCode],[ScoreCate],[Score] FROM [TraineeScore] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTraineeScore = "INSERT INTO [TraineeScore]([StdCode],[ProjectCode],[ScoreCate],[Score]) values(@StdCode,@ProjectCode,@ScoreCate,@Score);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTraineeScore = "UPDATE [TraineeScore] SET [StdCode] = @StdCode,[ProjectCode] = @ProjectCode,[ScoreCate] = @ScoreCate,[Score] = @Score WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTraineeScore = "DELETE [TraineeScore] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTraineeScore = "SELECT [Id], [StdCode],[ProjectCode],[ScoreCate],[Score] FROM [TraineeScore]";
		#endregion

		#region ITraineeScoreProvider Members
		public Core.Business.TraineeScore Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTraineeScore);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TraineeScore traineeScore = new Core.Business.TraineeScore();

				if (!reader.IsDBNull(0)) traineeScore.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) traineeScore.StdCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) traineeScore.ProjectCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) traineeScore.ScoreCate = reader.GetString(3);
				if (!reader.IsDBNull(4)) traineeScore.Score = reader.GetDecimal(4);

				return traineeScore;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TraineeScore traineeScore)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, traineeScore.StdCode);
			sql.AddParameter("@ProjectCode", SqlDbType.NVarChar, traineeScore.ProjectCode);
			sql.AddParameter("@ScoreCate", SqlDbType.NVarChar, traineeScore.ScoreCate);
			sql.AddParameter("@Score", SqlDbType.Decimal, traineeScore.Score);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTraineeScore);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) traineeScore.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TraineeScore traineeScore)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, traineeScore.StdCode);
			sql.AddParameter("@ProjectCode", SqlDbType.NVarChar, traineeScore.ProjectCode);
			sql.AddParameter("@ScoreCate", SqlDbType.NVarChar, traineeScore.ScoreCate);
			sql.AddParameter("@Score", SqlDbType.Decimal, traineeScore.Score);
			sql.AddParameter("@Id", SqlDbType.Int, traineeScore.Id);
			sql.ExecuteSql(SqlUpdateTraineeScore);
		}

		public void Delete(Core.Business.TraineeScore traineeScore)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, traineeScore.Id);
			sql.ExecuteSql(SqlDeleteTraineeScore);
		}

		public IList<Core.Business.TraineeScore> GetAllTraineeScore()
		{
			IList<Core.Business.TraineeScore> traineeScorelist = new List<Core.Business.TraineeScore>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTraineeScore);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TraineeScore traineeScore = new Core.Business.TraineeScore();

					if (!reader.IsDBNull(0)) traineeScore.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) traineeScore.StdCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) traineeScore.ProjectCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) traineeScore.ScoreCate = reader.GetString(3);
					if (!reader.IsDBNull(4)) traineeScore.Score = reader.GetDecimal(4);

					traineeScore.MarkOld();
					traineeScorelist.Add(traineeScore);
				}
				reader.Close();
			}
			return traineeScorelist;
		}
		#endregion
	}
}
